﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Fashion_API;

namespace A
{
    public partial class M
    {
        public static void Redirect(string url)
        {
            HttpContext.Current.Response.Redirect(url);
        }

        public static void SaveAccountLoginToCookies(Account account)
        {
            HttpCookie cookie = new HttpCookie(A.CookieName.Account);
            cookie[A.CookieName.UserName] = account.UserName;
            cookie[A.CookieName.UserRoleId] = account.UserRoleId.ToString();
            cookie[A.CookieName.UserId] = account.Id.ToString();
            cookie.Expires = DateTime.Today.AddDays(30);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static Account GetAccountFromCookies()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[A.CookieName.Account];
            if (cookie != null && cookie[A.CookieName.UserId] != null)
            {
                Account account = new Account();
                account.Id = Convert.ToInt32(cookie[A.CookieName.UserId]);
                account.UserName = cookie[A.CookieName.UserName];
                account.UserRoleId = Convert.ToInt32(cookie[A.CookieName.UserRoleId]);
                return account;
            }
            return null;
        }

        public static void DeleteAccountLoginInCookies()
        {
            HttpCookie cookie = new HttpCookie(A.CookieName.Account);
            cookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static void RefreshCurrentPage()
        {
            Redirect(HttpContext.Current.Request.Url.ToString());
        }

        public static string GetQueryString(string name)
        {
            return HttpContext.Current.Request.QueryString[name];
        }

        public static void BindDataComboBoxFromEnum(DropDownList cbo,Type type)
        {
            string[] allNames = type.GetEnumNames();
            var arrays = type.GetEnumValues();
            List<ListItem> listItems = new List<ListItem>();
            for (int i = 0; i < allNames.Length; i++)
            {
                ListItem item = new ListItem(allNames[i],arrays.GetValue(i).ToString());
                listItems.Add(item);
            }
            cbo.DataSource = listItems;
            cbo.DataTextField = "Text";
            cbo.DataValueField = "Value";
            cbo.DataBind();
        }
    }
}
